<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">报工单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="报工单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">开工单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="workOrderCode" class="layui-input" type="text" placeholder="开工单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">物料名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuName" class="layui-input" type="text" placeholder="物料名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">物料编码：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="物料名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">所属工序：</label>
                                    <div class="layui-input-inline">
                                        <select name="processId">
                                        </select>
                                    </div>
                                </div>
                                <!--状态:tosubmit(0,待提交);confirm(1,已提交);checked(2,已审核)-->
                                <div class="layui-inline">
                                    <label class="layui-form-label">报工单状态：</label>
                                    <div class="layui-input-inline">
                                        <select name="status">
                                            <option value="">工单状态</option>
                                            <option value="tosubmit">待提交</option>
                                            <option value="confirm">已提交</option>
                                            <option value="checked">已审核</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit>查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" type="reset" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button type="button" id="productionTheWorkOrderBtnAdd"
                                    class="layui-btn icon-btn color-reseda">添加报工单
                            </button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="productionTheWorkOrderTable"
                   lay-filter="productionTheWorkOrderTable"></table>
        </div>
    </div>
</div>

<style>
    .msg-none .layui-none {
        display: none !important;
    }
    .productionTheWorkOrderAddForm .layui-form-item .layui-input-inline {
        width: 290px;
    }
    .productionTheWorkOrderAddForm .layui-table-cell{
        height: auto;
    }
</style>

<!-- 表格操作列 -->
<script type="text/html" id="productionTheWorkOrderTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
    {{# if(d.status == "tosubmit"){  }}
    <a class="layui-btn layui-btn-xs" lay-event="submit">确认提交</a>
    {{# }else if(d.status =="confirm"){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="check">审核</a>
    {{# } }}
</script>
<!--工序表格操作列-->
<script type="text/html" id="theProcessTableBar">
    {{# if(d.isSave == "no"){  }}
    <a class="layui-btn layui-btn-xs" lay-event="save">保存</a>
    {{# }else if(d.isSave =="yes"){ }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    {{# } }}
</script>

<!--添加报工单-->
<script id="productionTheWorkOrderTemplate" type="text/html">
    <form lay-filter="productionTheWorkOrderAddForm"
          class="layui-form model-form form-scroll productionTheWorkOrderAddForm">
        <div class="open-details-box pd-20">
            <div class="layui-form-item" style="margin: 20px 0;">
                <label class="layui-form-label w-auto">选择开工单：</label>
                <div class="layui-input-inline">
                    <input name="productionPlanId" id="productionPlanId" class="layui-input" type="text"
                           placeholder="请选择开工单" readonly="readonly" style="cursor: pointer;"/>
                </div>
            </div>
            <div class="msg-none table-bor" id="isWorkOrderPlanId" style="display: none;">
                <div class="layui-form-item">
                    <div class="layui-form-item" style="margin-bottom: 10px;">
                        <div class="open-details-title">开工单信息</div>
                    </div>
                    <table class="layui-table" id="theWorkOrderTable"
                           lay-filter="theWorkOrderTable"></table>
                </div>
                <div class="layui-form-item">
                    <div class="layui-form-item" style="margin-bottom: 10px;">
                        <div class="open-details-title">工序报工信息</div>
                    </div>
                    <table class="layui-table" id="theProcessTable"
                           lay-filter="theProcessTable"></table>
                </div>
                <div class="layui-form-item" style="margin-top: 20px;">
                    <label class="layui-form-label w-auto">备注：</label>
                    <div class="layui-input-inline">
                        <input name="remark" class="layui-input" type="text"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right pd-20">
            <button class="layui-btn isSubmit" data-type="0" lay-filter="productionTheWorkOrderAddFormSubmit" lay-submit style="display: none;">保存</button>
            <button class="layui-btn layui-btn-primary closeBtn" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" data-type="1" lay-filter="productionTheWorkOrderAddFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!--选择开工单-->
<script type="text/html" id="selectTheWorkProcessTemplate">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">开工单编号：</label>
                            <div class="layui-input-inline mr0" style="width: 240px;">
                                <input name="code" class="layui-input" type="text" placeholder="开工单编号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="selectTheWorkProcessFormSubSearchLog"
                                    lay-submit style="padding: 0 24px;">
                                搜索
                            </button>
                            <button id="selectTheWorkProcessClose" class="layui-btn icon-btn">确认选择</button>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="selectTheWorkProcessTable"
                       lay-filter="selectTheWorkProcessTable"></table>
            </div>
        </div>
    </div>
</script>

<!--时间选择下拉框-->
<script type="text/html" id="startTimeTemplate">
    {{# if(d.isSave == "no"){  }}
        <form lay-filter="startTimeTypeForm" class="layui-form model-form" style="padding: 0;">
            <input type="text" name="startTime" id="startTime" value="{{d.startTime}}"  placeholder="请选择开始时间" autocomplete="off"  class="layui-input" style="height: 32px;">
        </form>
    {{# }else if(d.isSave =="yes"){ }}
        <span>{{d.startTime}}</span>
    {{# } }}
</script>

<!--审核报工单&&报工单详情-->
<script type="text/html" id="productionTheWorkOrderCheckTemplate">
    <form lay-filter="productionTheWorkOrderCheckForm"
          class="layui-form model-form form-scroll productionTheWorkOrderAddForm">
        <div class="open-details-box pd-20">
            <div class="layui-form-item" style="margin: 20px 0;">
                <label class="layui-form-label w-auto">报工单编号：</label>
                <div class="layui-input-inline">
                    <input name="code" class="layui-input br0" type="text" readonly="readonly"/>
                </div>
            </div>
            <div class="msg-none table-bor">
                <div class="layui-form-item">
                    <div class="layui-form-item" style="margin-bottom: 10px;">
                        <div class="open-details-title">开工单信息</div>
                    </div>
                    <table class="layui-table" id="theWorkOrderCheckTable"
                           lay-filter="theWorkOrderCheckTable"></table>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label w-auto">开工单备注：</label>
                    <div class="layui-input-inline">
                        <input name="workRemark" class="layui-input br0" type="text" readonly="readonly"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-form-item" style="margin-bottom: 10px;">
                        <div class="open-details-title">工序报工信息</div>
                    </div>
                    <table class="layui-table" id="theProcessCheckTable"
                           lay-filter="theProcessCheckTable"></table>
                </div>
                <div class="layui-form-item" style="margin-top: 20px;">
                    <label class="layui-form-label w-auto">备注说明：</label>
                    <div class="layui-input-inline">
                        <input name="remark" class="layui-input" type="text"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label w-auto">审核状态：</label>
                    <div class="layui-input-inline">
                        <select name="checkStatus" id="checkStatus" lay-filter="checkStatus"
                                lay-verify="required">
                            <option value="">请选择</option>
                            <option value="correspond">审核相符</option>
                            <option value="discrepancy">审核不符</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right pd-20">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn isCheck" lay-filter="productionTheWorkOrderCheckFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        // 渲染表格
        var insTb = table.render({
            elem: '#productionTheWorkOrderTable',
            url: config.base + 'production/productionPlan/admin/productionTheWorkOrder/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', align: 'center'}
                , {field: 'code', title: '报工单编码', align: 'center'}
                , {field: 'processName', title: '所属工序', align: 'center'}
                , {field: 'workOrderCode', title: '所属开工单', align: 'center'}
                , {field: 'workOrderQualityDesc', title: '开工单性质', align: 'center'}
                , {field: 'deviceCode', title: '责任工站', align: 'center'}
                , {field: 'skuName', title: '物料名称', align: 'center'}
                , {field: 'skuCode', title: '物料编码', align: 'center'}
                , {field: 'productTypeName', title: '所属系列', align: 'center'}
                , {field: 'workQuantity', title: '报工数量', align: 'center'}
                , {field: 'qualityQuantity', title: '质检数量', align: 'center'}
                , {field: 'qualifiedQuantity', title: '合格数量', align: 'center'}
                , {field: 'finishDate', title: '预计完工时间', align: 'center'}
                , {field: 'statusDesc', title: '状态', align: 'center'}
                , {align: 'center', toolbar: '#productionTheWorkOrderTableBar', title: '操作', width: 140}
            ]],
        });

        // 工具条点击事件
        table.on('tool(productionTheWorkOrderTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'submit') { //确认提交
                productionTheWorkOrderAddModel('submit', data.id);
            } else if (obj.event === 'check') { //审核
                checkTheWorkOrderModel('check', data.id);
            } else if (obj.event === 'detail') { //详情
                checkTheWorkOrderModel('detail', data.id);
            }
        });
        form.render('select');
        //监听排序
        table.on('sort(productionTheWorkOrderTable)', function (obj) {
            table.reload('productionTheWorkOrderTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });
        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].productType}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 3) {
                    html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
                }else if(type == 4){
                    html += `<option value='${data[i].skuCode}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if(type == 5){
                    html += `<option value='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if(type == 6){
                    html += `<option value='${data[i].id}'>${data[i].nodeNames}</option>`;
                }

            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }
        //查询工序
        admin.req('enterprise/product/admin/processInfo/getAllProcessInfo', {}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0){
                    loadSelectHtml(res.data, "所属工序", "processId", 6);
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
            return false;
        });

        // 添加按钮点击事件
        $('#productionTheWorkOrderBtnAdd').click(function () {
            productionTheWorkOrderAddModel('add', '');
        });

        // 添加报工单
        function productionTheWorkOrderAddModel(type, theWorkOrderId) {
            admin.open({
                type: 1,
                area: ['82%', '840px'],
                offset: ['65px', '280px'],
                title: type == 'add' ? '添加报工单信息' : '确认提交报工单信息',
                content: $('#productionTheWorkOrderTemplate').html(),
                success: function () {
                    let addTableData = [];
                    let workOrderId = '';
                    // 选择开工单（添加开工单）
                    $("#productionPlanId").click(function () {
                        if (type == 'add') {
                            var selectTheWorkProcessTemplate = admin.open({
                                type: 1,
                                title: '开工单列表',
                                area: '75%',
                                offset: ['160px','320px'],
                                content: $('#selectTheWorkProcessTemplate').html(),
                                success: function () {
                                    form.render("select");
                                    // 渲染表格
                                    var selectTheWorkProcessTable = table.render({
                                        elem: '#selectTheWorkProcessTable',
                                        url: config.base + 'production/productionPlan/admin/productionWorkOrder/list',
                                        page: true,
                                        cellMinWidth: 100,
                                        cols: [[
                                            {type: 'radio'}
                                            , {field: 'code', align: 'center', title: '工单编码', width: 150}
                                            , {field: 'processName', align: 'center', title: '所属工序'}
                                            , {field: 'planCode', align: 'center', title: '所属生产计划', width: 160}
                                            , {field: 'workOrderQualityDesc', align: 'center', title: '工单性质'}
                                            , {field: 'deviceCode', align: 'center', title: '责任工站'}
                                            , {field: 'skuCode', align: 'center', title: '物料编码'}
                                            ,{field: 'productionQuantity', align: 'center', title: '排产数量'}
                                            ,{field: 'finishQuantity', align: 'center', title: '完成数量'}
                                            // , {field: 'predictStartTime', align: 'center', title: '预计开始时间'}
                                            // , {field: 'predictFinishTime', align: 'center', title: '预计完工时间'}
                                            , {field: 'statusDesc',align: 'center',  title: '状态'}
                                        ]],
                                    });

                                    // 搜索
                                    form.on('submit(selectTheWorkProcessFormSubSearchLog)', function (data) {
                                        selectTheWorkProcessTable.reload({
                                            where: data.field,
                                            page: {curr: 1}
                                        }, 'data');
                                    });

                                    // 确认选择
                                    $('#selectTheWorkProcessClose').click(function () {
                                        let checkStatus = table.checkStatus('selectTheWorkProcessTable'); //test即为基础参数id对应的值
                                        let dataS = checkStatus.data[0];
                                        if (checkStatus.data.length == 0) {
                                            layer.msg("请选择开工单", {icon: 2, time: 1000});
                                        } else {
                                            $("#isWorkOrderPlanId").show();
                                            $("#productionPlanId").val(dataS.code);
                                            workOrderId = dataS.id;
                                            getWorkOrderDetails(workOrderId);
                                            layer.close(selectTheWorkProcessTemplate);
                                        }
                                    });
                                }
                            })
                        }
                    });

                    // 获取开工单信息和工序信息
                    function getWorkOrderDetails(id) {
                        admin.req('production/productionPlan/admin/productionWorkOrder/getWorkOrderDetails', {
                            workOrderId: id
                        }, function (res) {
                            if (res.code == 200) {
                                let dataS = res.data;
                                dataS.isSave = 'no';
                                dataS.workQuantity = '';
                                dataS.qualityQuantity = '';
                                dataS.qualifiedQuantity = '';
                                dataS.startTime = dateTime(new Date()); //当前时间
                                $("input[name='workRemark']").val(dataS.remark);
                                addTableData.push(dataS);
                                // 渲染表格
                                table.reload('theWorkOrderTable', {
                                    data: addTableData,
                                });
                                table.reload('theProcessTable', {
                                    data: addTableData,
                                });
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 确认提交开工单
                    if (type == 'submit') {
                        $(".closeBtn").show();
                        $(".isSubmit").hide();
                        admin.req('production/productionPlan/admin/productionTheWorkOrder/selectTheWorkOrderInfo', {
                            id: theWorkOrderId
                        }, function (res) {
                            if (res.code == 200) {
                                let dataS = res.data[0];
                                $("#isWorkOrderPlanId").show();
                                $("#productionPlanId").val(dataS.workOrderCode);
                                $("input[name='remark']").val(dataS.remark);
                                $("input[name='workRemark']").val(dataS.workOrderRemark);
                                dataS.isSave = 'yes'
                                addTableData.push(dataS);
                                // 渲染表格
                                table.reload('theWorkOrderTable', {
                                    data: addTableData,
                                });
                                table.reload('theProcessTable', {
                                    data: addTableData,
                                });
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    } else {
                        $(".closeBtn").hide();
                        $(".isSubmit").show();
                    }

                    // 开工单信息
                    table.render({
                        elem: '#theWorkOrderTable',
                        data: addTableData,
                        cellMinWidth: 100,
                        page: false,
                        cols: [[
                            {field: 'planCode', title: '所属生产计划', align: 'center'}
                            , {field: 'processName', title: '排产工序', align: 'center'}
                            , {field: 'productTypeName', title: '物料类型', align: 'center'}
                            , {field: 'skuCode', title: '物料编号', align: 'center'}
                            , {field: 'skuName', title: '物料名称', align: 'center'}
                            , {field: 'skuSpec', title: '内部规格码', align: 'center'}
                            , {field: 'productionQuantity', title: '排产数量', align: 'center'}
                            , {field: 'finishQuantity', title: '已完工数量', align: 'center'}
                        ]]
                    });

                    // 工序报工信息
                    table.render({
                        elem: '#theProcessTable',
                        data: addTableData,
                        cellMinWidth: 100,
                        page: false,
                        cols: [[
                            {field: 'startTime', title: '开始时间', templet: "#startTimeTemplate", width: 220, align: 'center'}
                            , {field: 'creatorName', title: '报工人', align: 'center'}
                            , {
                                field: 'workQuantity',
                                title: '报工数量',
                                align: 'center',
                                width: 165,
                                templet: function (d) {
                                    if (d.isSave == 'yes') {
                                        return '<span>' + d.workQuantity + '</span>'
                                    } else {
                                        return '<input deviceId="' + d.id + '" class="layui-input workQuantity" style="height: 32px;" type="text" ' +
                                            'value="' + d.workQuantity + '" lay-verify="required"/>'
                                    }
                                }
                            }
                            , {
                                field: 'qualifiedQuantity',
                                title: '质检数',
                                align: 'center',
                                width: 165,
                                templet: function (d) {
                                    if (d.isSave == 'yes') {
                                        return '<span>' + d.qualifiedQuantity + '</span>'
                                    } else {
                                        return '<input deviceId="' + d.id + '" class="layui-input qualifiedQuantity" style="height: 32px;" type="text" ' +
                                            'value="' + d.qualifiedQuantity + '" lay-verify="required"/>'
                                    }
                                }
                            }
                            , {
                                field: 'qualityQuantity',
                                title: '合格数',
                                align: 'center',
                                width: 165,
                                templet: function (d) {
                                    if (d.isSave == 'yes') {
                                        return '<span>' + d.qualityQuantity + '</span>'
                                    } else {
                                        return '<input deviceId="' + d.id + '" class="layui-input qualityQuantity" style="height: 32px;" type="text" ' +
                                            'value="' + d.qualityQuantity + '" lay-verify="required"/>'
                                    }
                                }
                            }
                            , {field: 'yield', title: '合格率', align: 'center'}
                            , {field: 'pieceRate', title: '计件工资', align: 'center'}
                            , {field: 'createTime', title: '报工时间', align: 'center'}
                            , {
                                align: 'center',
                                toolbar: '#theProcessTableBar',
                                title: '操作',
                                width: 80
                            }
                        ]]
                    });

                    // 报工数量
                    $("body").delegate(".workQuantity", "change", function () {
                        regQuantity(".workQuantity")
                    });

                    // 质检数量
                    $("body").delegate(".qualifiedQuantity", "change", function () {
                        regQuantity(".qualifiedQuantity")
                    });

                    // 合格数量
                    $("body").delegate(".qualityQuantity", "change", function () {
                        regQuantity(".qualityQuantity")
                    });

                    function regQuantity(dom) {
                        let reg = /^[1-9]+[0-9]*$/;
                        let value = $(dom).val();
                        if (!reg.test(value)) {
                            layer.msg('请输入正确的数字', {icon: 2});
                            $(dom).val('');
                        }
                    }

                    renderTime();

                    // 工序报工信息操作系列
                    table.on("tool(theProcessTable)", function (obj) {
                        if (obj.event === 'save') { //保存
                            let workQuantity = $(this).parents('td').siblings().find('.workQuantity').val();
                            let qualifiedQuantity = $(this).parents('td').siblings().find('.qualifiedQuantity').val();
                            let qualityQuantity = $(this).parents('td').siblings().find('.qualityQuantity').val();
                            if (workQuantity == '') {
                                layer.msg('报工数量不能为空', {icon: 2});
                                return false;
                            }
                            if (qualifiedQuantity == '') {
                                layer.msg('质检数量不能为空', {icon: 2});
                                return false;
                            }
                            if (qualityQuantity == '') {
                                layer.msg('合格数不能为空', {icon: 2});
                                return false;
                            }
                            addTableData[0].isSave = 'yes';
                            addTableData[0].workQuantity = workQuantity;
                            addTableData[0].qualifiedQuantity = qualifiedQuantity;
                            addTableData[0].qualityQuantity = qualityQuantity;
                        } else if (obj.event === 'edit') {
                            addTableData[0].isSave = 'no'
                        }
                        // 渲染表格
                        table.reload('theProcessTable', {
                            data: addTableData,
                        });
                        renderTime();
                    });

                    // 开始时间
                    function renderTime() {
                        laydate.render({
                            elem: '#startTime'
                            ,type: 'datetime'
                            ,done: function(value){
                                addTableData[0].startTime = value
                            }
                        });
                    }

                    // 提交数据
                    form.on('submit(productionTheWorkOrderAddFormSubmit)', function (d) {
                        let addType = $(this).data('type');
                        let dataS = addTableData[0];
                        if (type == 'add') {
                            if (workOrderId == '') {
                                layer.msg("请选择开工单", {icon: 2});
                                return false;
                            }
                            if (dataS.isSave == 'no') {
                                layer.msg("请保存工序报工信息", {icon: 2});
                                return false;
                            }
                            let params = {
                                workQuantity: dataS.workQuantity,
                                qualifiedQuantity: dataS.qualifiedQuantity,
                                qualityQuantity: dataS.qualityQuantity,
                                startTime: dataS.startTime,
                                remark: d.field.remark,
                                workOrderId: workOrderId,
                                addType: addType
                            }
                            layer.load(2);
                            admin.req('production/productionPlan/admin/productionTheWorkOrder/add', JSON.stringify(params), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('productionTheWorkOrderTable');
                                    layer.closeAll();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'POST', true, 'application/json; charset=utf-8');
                        } else if (type == 'submit') {
                            if (dataS.isSave == 'no') {
                                layer.msg("请保存工序报工信息", {icon: 2});
                                return false;
                            }
                            let params = {
                                workQuantity: dataS.workQuantity,
                                qualifiedQuantity: dataS.qualifiedQuantity,
                                qualityQuantity: dataS.qualityQuantity,
                                startTime: dataS.startTime,
                                remark: d.field.remark,
                                id: dataS.id,
                                status: 'confirm'
                            }
                            layer.load(2);
                            admin.req('production/productionPlan/admin/productionTheWorkOrder/update', JSON.stringify(params), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('productionTheWorkOrderTable');
                                    layer.closeAll();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'PUT', true, 'application/json; charset=utf-8');
                        }
                        return false;
                    });

                }
            });
        }

        // 审核报工单
        function checkTheWorkOrderModel(type, theWorkOrderId) {
            admin.open({
                type: 1,
                area: ['82%', '840px'],
                offset: '65px',
                title: type == 'detail' ? '报工单详情' : '审核报工单',
                content: $('#productionTheWorkOrderCheckTemplate').html(),
                success: function () {
                    let list = [];
                    let dataS = {};
                    admin.reqSync('production/productionPlan/admin/productionTheWorkOrder/selectTheWorkOrderInfo', {
                        id: theWorkOrderId
                    }, function (res) {
                        if (res.code == 200) {
                            dataS = res.data[0];
                            list = res.data;
                            $("input[name='code']").val(dataS.code);
                            $("input[name='remark']").val(dataS.remark);
                            $("input[name='workRemark']").val(dataS.workOrderRemark);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    form.render('select');

                    if (type == 'check') {
                        $(".isCheck").show();
                    } else {
                        if (Object.keys(dataS.checkStatus).length > 0) {
                            let select = 'dd[lay-value=' + dataS.checkStatus + ']';
                            $('#checkStatus').siblings("div.layui-form-select").find('dl').find(select).click();
                            $('#checkStatus').attr('disabled', 'disabled');
                            form.render('select');
                        }
                        $(".isCheck").hide();
                    }

                    // 开工单信息
                    table.render({
                        elem: '#theWorkOrderCheckTable',
                        data: list,
                        cellMinWidth: 100,
                        page: false,
                        cols: [[
                            {field: 'planCode', title: '所属生产计划', align: 'center'}
                            , {field: 'productTypeName', title: '物料类型', align: 'center'}
                            , {field: 'skuCode', title: '物料编号', align: 'center'}
                            , {field: 'skuName', title: '物料名称', align: 'center'}
                            , {field: 'skuSpec', title: '内部规格码', align: 'center'}
                            , {field: 'unitName', title: '单位', align: 'center'}
                            , {field: 'processName', title: '排产工序', align: 'center'}
                            , {field: 'productionQuantity', title: '排产数量', align: 'center'}
                            , {field: 'finishQuantity', title: '已完工数量', align: 'center'}
                        ]]
                    });

                    // 工序信息
                    table.render({
                        elem: '#theProcessCheckTable',
                        data: list,
                        cellMinWidth: 100,
                        page: false,
                        cols: [[
                            {field: 'workOrderCode', title: '所属开工单', align: 'center'}
                            , {field: 'startTime', title: '开始时间', align: 'center'}
                            , {field: 'creatorName', title: '报工人', align: 'center'}
                            , {field: 'workQuantity', title: '报工数量', align: 'center'}
                            , {field: 'qualifiedQuantity', title: '质检数', align: 'center'}
                            , {field: 'qualityQuantity', title: '合格数', align: 'center'}
                            , {field: 'yield', title: '合格率', align: 'center'}
                            , {field: 'pieceRate', title: '计件工资', align: 'center'}
                            , {field: 'createTime', title: '报工时间', align: 'center'}
                        ]]
                    });

                    form.on('submit(productionTheWorkOrderCheckFormSubmit)', function (d) {
                        let params = {
                            workQuantity: dataS.workQuantity,
                            qualifiedQuantity: dataS.qualifiedQuantity,
                            qualityQuantity: dataS.qualityQuantity,
                            startTime: dataS.startTime,
                            remark: d.field.remark,
                            id: dataS.id,
                            checkStatus: d.field.checkStatus,
                            status: 'checked'
                        }
                        layer.load(2);
                        admin.req('production/productionPlan/admin/productionTheWorkOrder/update', JSON.stringify(params), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('productionTheWorkOrderTable');
                                layer.closeAll();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'PUT', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }
            })
        }

        const dateTime = (date) => {
            let Y = date.getFullYear(),
                M = date.getMonth() + 1,
                D = date.getDate(),
                H = date.getHours(),
                S = date.getMinutes();
            if (M < 10) {
                M = '0' + M;
            }
            if (D < 10) {
                D = '0' + D;
            }
            if (H < 10) {
                H = '0' + H;
            }
            if (S < 10) {
                S = '0' + S;
            }
            return `${Y}-${M}-${D} ${H}:${S}:00`
        }

    });
</script>